<?php
    class user_model extends Model
    {
        public function __construct(){
            parent::__construct();
            $this->setTableName('users');
            $this->load->library('auth');
        }
        
        /*
            @param string name name of user want to regist or login
            @return boolean whether the user is exist or not
        */
        public function checkAvail($name){
            $info = $this->select(
                array('username'), array('username' => $name));
            return empty($info);
        }
        
        /*
            @param string name name of user want to login
            @param string pwd password who wants to use as an identification
            @return boolean state of login attempt
        */
        public function login($name, $pwd, $remeber = false){
            //check avail
            return auth('login', $name, $pwd, $remeber);
        }
        
        /*
            @param string name name of user want to regist
            @param string pwd password of user wants to use as an identification
            @param array info informaton of user want to store in db
            @return int? false when regist failed int when insert success
        */
        public function regist($name, $pwd, $info = null){
            //check avail
            if (!$this->checkAvail($name))
                return false;
            
            if (!empty($info)){
                //setting default values
                $pwd = $this->_encryptPassword($pwd);
               	$info['password'] = $pwd;
                $info['username'] = $name;
                $info['activated'] = 0;
                return $this->insert($info);
            }
            else return false;
        }
        
        /*
            @return boolean state of logoff action state
        */
        public function logoff(){
            return auth('logoff');
        }
        
        /*
        	@param int? value of user's id to modify info
        	@param array array of info to modify
        	@return boolean modify info action state
        */
        public function modifyInfo($id, $info){
        	$id = intval($id);
            
        	if (empty($id)) $id = $this->getUserId();
            
            if (empty($id)) return false;
            
            if ($this->exists($id)) return false;
        
        	if (empty($info))
            	return false;
            
            if (isset($info['password']))
            	$info['password'] = $this->_encryptPassword($info['password']);
            
            if (isset($info['username']))
            	unset($info['username']);
                
            return $this->update($info, array('id' => $id));
        }
        
        /*
            @return boolean state of whether user is logged in
        */
        public function isLogin(){
            return auth('checkLogin');
        }
        
        /*
            @return int? current login user id (int? is combination of int and null)
        */
        public function getUserId(){
            if (auth('checkLogin')){
                //logged in
                //fetch id from session 
                return intval($_SESSION['userInfo']['userId']);
            }
            else
                return null;
        }
        
        /*
        	@param int user's id
            @return array? user's basic info
        */
        public function getUserInfo($id = null){
       		//assert valid user id
            $id = intval($id);
            
            if (empty($id)) $id = $this->getUserId();
            
            if (empty($id)) return null;
            
            $sql = 'select * from ' . $this->getTableName()
            	. ' where id = {0} limit 1';
            
            return $this->db->fetchOne($sql, array($id));
        }
        
        /*
            @return array current registed users' list, can be empty
        */
        public function getUserList(){
            return $this->select();
        }
        
        /*
            @return bool status of the account activate action
        */
        public function activate($id){
        	return $this->_activate($id, true);
        }
        
        /*
        	@return bool return state of deactive action
        */
        public function deactivate($id){
        	return $this->_activate($id, false);
        }
        
        /*
        	@param id int userId default: current logged in userid
            @return bool state of the specific user existence
        */
        public function exists($id = null){
        	$info = $this->getUserInfo($id);
            
            return !empty($info);
        }
        
        /*
        	@param int? user's id to activate or deactivate
        	@param bool record activated bit state
        	@return bool db activated bit set and reset
        */
        private function _activate($id, $state){
            $id = intval($id);
            
            if (empty($id)) $id = $this->getUserId();
            
            if (empty($id)) return false;
            
            $state = $state ? 1 : 0;
            return $this->update(
            	array('activated' => $state), array('id' => $id));
        }
        
        /*
        	@param string password string in plain viewing state
        	@return string password string in hash function working out state
        */
        private function _encryptPassword($pwd){
        	//plain md5 encrypt, with lower case alpha
        	return md5($pwd);
        }
    }
